package cn.cvs.controller;

import cn.cvs.pojo.StorageRecord;
import cn.cvs.pojo.Supplier;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.StorageRecordService;
import cn.cvs.service.SupplierService;
import cn.cvs.utils.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/sys/storageRecord")
@Controller
public class  StorageController {

    @Resource
    private StorageRecordService storageRecordService;
    @Resource
    private SupplierService supplierService;

    @RequestMapping("/list")
    public String list(Model model, String queryGoodsName, Integer querySupplierId, Integer queryPayStatus, @RequestParam(required = false, defaultValue = "1") Integer pageIndex) {

        //查询全部供货商
        List<Supplier> supplierList = supplierService.getSupplierList();
        //查询入库数量
        int totalCount = storageRecordService.getStorageRecordByIf(querySupplierId, queryPayStatus, queryGoodsName);

        System.out.println("StorageCount-========:"+totalCount);
        //每页数量
        int pageSize = 5;
        PageSupport p=new PageSupport();
        p.setCurrentPageNo(pageIndex);
        p.setPageSize(pageSize);
        p.setTotalCount(totalCount);
        //总页数
        int totalPageCount=p.getTotalPageCount();
        //控制首页和尾页
        if (pageIndex > totalPageCount) {
            pageIndex = totalPageCount;
            throw new RuntimeException("页码不正确");
        }
        //根据条件分页查询入库信息
        List<StorageRecord> storageRecordList = storageRecordService.getListByPage(querySupplierId, queryPayStatus, queryGoodsName, pageIndex, pageSize);

        model.addAttribute("supplierList",supplierList);
        model.addAttribute("storageRecordList",storageRecordList);
        //总页数
        model.addAttribute("totalPageCount", totalPageCount);
        //查询总数量
        model.addAttribute("totalCount", totalCount);
        //当前页码
        model.addAttribute("currentPageNo", pageIndex);
        //查询的商品名
        model.addAttribute("queryGoodsName", queryGoodsName);
        //查询的供货商id
        model.addAttribute("querySupplierId", querySupplierId);
        //查询的付款状态
        model.addAttribute("querypayStatus", queryPayStatus);

        return "storageRecord/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "storageRecord/add";
    }

    @RequestMapping("/add")
    public String add(StorageRecord storageRecord, HttpSession session){
        storageRecord.setCreatedTime(new Date());
        storageRecord.setCreatedUserId(((SysUser) session.getAttribute("userSession")).getId());
        int a=storageRecordService.addStorageRecord(storageRecord);
        if(a>0) {
            return "redirect:/sys/storageRecord/list";
        }else{
            return "storageRecord/add";
        }
    }

    @RequestMapping("/toUpdate/{sId}")
    public String toUpdate(@PathVariable Integer sId,Model model){
        StorageRecord storageRecord=storageRecordService.getStorageById(sId);
        model.addAttribute("storageRecord",storageRecord);
        return "storageRecord/update";
    }


    @RequestMapping("/update")
    public String update(StorageRecord storageRecord,HttpSession session){
        storageRecord.setUpdatedTime(new Date());
        storageRecord.setUpdatedUserId(((SysUser) session.getAttribute("userSession")).getId());
        int a=storageRecordService.updStorageRecord(storageRecord);
        if(a>0){
            return "redirect:/sys/storageRecord/list";
        }else{
            return "storageRecord/update";
        }
    }

    @RequestMapping("/view/{sId}")
    public String view(@PathVariable Integer sId,Model model){
        StorageRecord storageRecord=storageRecordService.getStorageById(sId);
        model.addAttribute("storageRecord",storageRecord);
        return "storageRecord/view";
    }

    @RequestMapping("/del/{sId}")
    @ResponseBody
    public Object del(@PathVariable Integer sId){
        System.out.println("storgggggg-+++++:"+sId);
        Map<String,Object> map=new HashMap<>();
        int a=-1;
        try {
            a=storageRecordService.delStorageRecord(sId);
            if(a>0){
                map.put("delResult","true");
            }else {
                map.put("delResult","false");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult","notexist");
        }
        return map;
    }



}
